package com.sky.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.Dao.CategoryDao;
import com.sky.Dao.DishDao;
import com.sky.Dao.SetmealDao;
import com.sky.Dao.SetmealDishDao;
import com.sky.Service.SetmealDishService;
import com.sky.Service.SetmealService;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.PageResult;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl
        extends ServiceImpl<SetmealDao, Setmeal>
        implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealDishDao setmealDishDao;
    @Autowired
    private DishDao dishDao;

    //TODO 分页逻辑还需要修改
    @Override
    public PageResult getByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        IPage<Setmeal> page = new Page<>(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        IPage<SetmealVO> setmealVOIPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //按照套餐名称模糊查询
        queryWrapper.like(setmealPageQueryDTO.getName() != null, Setmeal::getName, setmealPageQueryDTO.getName());
        //按照套餐分类查询
        queryWrapper.eq(setmealPageQueryDTO.getCategoryId() != null, Setmeal::getCategoryId, setmealPageQueryDTO.getCategoryId());
        //按照售卖状态查询
        queryWrapper.eq(setmealPageQueryDTO.getStatus() != null, Setmeal::getStatus, setmealPageQueryDTO.getStatus());
        queryWrapper.orderByAsc(Setmeal::getName).orderByDesc(Setmeal::getUpdateTime);
        setmealDao.selectPage(page, queryWrapper);
        BeanUtils.copyProperties(page, setmealVOIPage, "records");
        //单独来处理records属性
        List<Setmeal> records = page.getRecords();
        List<SetmealVO> setmealVOList = records.stream().map(item -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(item, setmealVO);

            Category category = categoryDao.selectById(item.getCategoryId());
            //获取分类名称
            String name = category.getName();
            //设置分类名称
            setmealVO.setCategoryName(name);
            return setmealVO;
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealVOIPage.getTotal());
        pageResult.setRecords(setmealVOList);
        return pageResult;

    }

    @Override
    public void addSetmealInfo(SetmealDTO setmealDTO) {
        //保存套餐的基本信息  操作setmeal表，insert
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal, "setmealDishes");
        //设置默认状态为上架
        setmeal.setStatus(StatusConstant.ENABLE);
        setmealDao.insert(setmeal);
        //保存套餐和菜品的关联信息  操作setmeal_dish表，insert
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.stream().forEach(item -> {
            //设置套餐id
            item.setSetmealId(setmeal.getId());
        });
        System.out.println("setmealDishes:" + setmealDishes.toString());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        //根据id查询套餐信息
        Setmeal setmeal = setmealDao.selectById(id);
        //修改套餐状态
        setmeal.setStatus(status);
        //修改更新时间
        setmeal.setUpdateTime(LocalDateTime.now());
        //更新修改人
        setmeal.setUpdateUser(BaseContext.getCurrentEmpId());
        //更新套餐信息
        setmealDao.updateById(setmeal);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        //新建一个SetmealVO对象
        SetmealVO setmealVO = new SetmealVO();
        //根据id查询套餐信息
        Setmeal setmeal = setmealDao.selectById(id);
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryDao.selectById(categoryId);
        //获取分类名称
        String name = category.getName();
        //属性拷贝
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setCategoryName(name);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(queryWrapper);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;


    }

    @Override
    public void modifySetmealInfo(SetmealVO setmealVO) {
        //更新套餐表的基本信息
        Setmeal setmeal = setmealDao.selectById(setmealVO.getId());
        //属性拷贝
        BeanUtils.copyProperties(setmealVO,setmeal,"categoryName,setmealDishes");
        //修改更新时间
        setmeal.setUpdateTime(LocalDateTime.now());
        //更新修改人
        setmeal.setUpdateUser(BaseContext.getCurrentEmpId());
        //保存修改后的套餐信息
        setmealDao.updateById(setmeal);
        //单独来处理菜品信息
        //清理当前套餐对应的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishDao.delete(queryWrapper);
        //添加当前提交过来的菜品信息
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes.stream().forEach(item->{
            //设置套餐id
            item.setSetmealId(setmealVO.getId());
            //修改更新时间
            item.setUpdateTime(LocalDateTime.now());
            //更新修改人
            item.setUpdateUser(BaseContext.getCurrentEmpId());
            //保存菜品信息
            setmealDishDao.insert(item);
        });
    }


    @Override
    public void deleteSetmealByIds(Long[] ids) {
        for(Long id:ids){
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,id);
            //删除套餐关联的菜品信息  操作setmeal_dish表，delete
            setmealDishDao.delete(queryWrapper);
            //删除套餐基本信息 操作setmeal表，delete
            setmealDao.deleteById(id);
        }
    }

    @Override
    public List<Setmeal> getSetmealListById(Long categoryId) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(Setmeal::getStatus,StatusConstant.ENABLE);
        queryWrapper.orderByDesc(Setmeal::getPrice).orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealDao.selectList(queryWrapper);
        return setmeals;
    }

    @Override
    public List<DishItemVO> getDishesByCategoryId(Long id) {
        //根据id获取套餐菜品列表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(queryWrapper);
        List<DishItemVO> dishItemVOList=setmealDishes.stream().map(item->{
            DishItemVO dishItemVO = new DishItemVO();
            Dish dish = dishDao.selectById(item.getDishId());
            //设置菜品名称
            dishItemVO.setName(item.getName());
            //设置菜品图片
            dishItemVO.setImage(dish.getImage());
            //设置菜品份数
            dishItemVO.setCopies(item.getCopies());
            //设置菜品描述
            dishItemVO.setDescription(dish.getDescription());
            return dishItemVO;
        }).collect(Collectors.toList());
        return dishItemVOList;
    }
}
